package daytrader.gui;

import java.util.*;

import java.awt.*;

import javax.swing.JPanel;

import daytrader.types.Hold;
import daytrader.types.Portfolio;
import daytrader.lib.LibData;

public class PortfolioGraph extends JPanel
{
    // drawing parameters
    public final int PADDING = 20;
    
    // reference to the gui object
    protected GUI gui;  
    protected ArrayList<ArrayList<StockHistoryData>> stockHistory;
    
    // temporaries for drawing
    private int w;
    private int h;
    private int ew;
    private int eh;
    private Graphics2D g2;
    
    private int amount_min;
    private int amount_max;
    private double profit_min;
    private double profit_max;
    
    private double current_price;
    
    public PortfolioGraph(GUI g)
    {
        gui = g;
        stockHistory = gui.getStockHistory();
        
    }
    
    protected void paintComponent(Graphics g)    
    {
        super.paintComponent(g);
        g2 = (Graphics2D)g;

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
       
        w = getWidth();
        h = getHeight();
        ew = w - 2 * PADDING;
        eh = h - 2 * PADDING;

        gui.libData.openStockFeed(gui.stockName);
        current_price = gui.libData.close();
        
        amount_min = 0;
        amount_max = 0;
        profit_min = Double.MAX_VALUE;
        profit_max = -Double.MAX_VALUE;
        for (int i = 0; i < stockHistory.size(); i++)
        {
            //if (portfolioHistory.get(i).stocks.size() > 0) System.out.println(portfolioHistory.get(i).stocks.get(0).company_name() + ": " + gui.stockName);
            
            StockHistoryData s = findStockInStockList(stockHistory.get(i), gui.stockName);
            int amount = 0;
            double profit = 0;
            if (s != null)
            {
                amount = s.amount;
                profit = s.accumulative_profit + s.amount * (s.price - s.bought_price);
            }
            if (amount > amount_max)
                amount_max = amount;
            if (profit > profit_max)
                profit_max = profit;
            if (profit < profit_min)
                profit_min = profit;
        }
        
        // draw grid
        g2.setColor(Color.BLACK);
        g2.drawLine(PADDING, PADDING, PADDING, h - PADDING);
        g2.drawLine(w - PADDING, PADDING, w - PADDING, h - PADDING);
        g2.drawLine(PADDING, h - PADDING, w - PADDING, h - PADDING);
        
        // draw portfolio amount
        drawAmount();
        
        // draw portfolio profit
        drawProfit();

    }

    protected void drawAmount()
    {
        g2.setColor(Color.BLUE);
        int lastx = 0;
        int lasty = 0;
        for (int i = 0; i < stockHistory.size(); i++)
        {
            int amount = 0;
            StockHistoryData s = findStockInStockList(stockHistory.get(i), gui.stockName);
            if (s != null)
                amount = s.amount;
            //System.out.println(amount);
            
            int x = (int)(PADDING + (gui.TICKS - stockHistory.size() + i + 0.5) * ew / gui.TICKS);
            int y = 0;
            if (amount_max != amount_min)   y = (int)(h - PADDING - (double)amount * eh / (amount_max - amount_min));
            else                            y = (int)(h - PADDING);
            
            if (i != 0)
            {
                g2.drawLine(lastx, lasty, lastx, y);
                g2.drawLine(lastx, y, x, y);
            } else
            {
                g2.drawLine(x, h - PADDING, x, y);
            }
            
            lastx = x;
            lasty = y;
        }

        g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
        g2.drawString(Integer.valueOf(amount_max).toString(), PADDING - 15, PADDING + 4);
        g2.drawString(Integer.valueOf(amount_min).toString(), PADDING - 15, h - PADDING + 4);
    }
 
    protected void drawProfit()
    {
        g2.setColor(Color.MAGENTA);
        int lastx = 0;
        int lasty = 0;
        for (int i = 0; i < stockHistory.size(); i++)
        {
            double profit = 0;
            StockHistoryData s = findStockInStockList(stockHistory.get(i), gui.stockName);
            if (s != null)
                profit = s.accumulative_profit + s.amount * (s.price - s.bought_price);
            //System.out.println(amount);
            
            int x = (int)(PADDING + (gui.TICKS - stockHistory.size() + i) * ew / gui.TICKS);
            int y = 0;
            if (profit_max != profit_min)   y = (int)(h - PADDING - (profit - profit_min) * eh / (profit_max - profit_min));
            else                            y = (int)(h / 2);
            
            if (i != 0)
                g2.drawLine(lastx, lasty, x, y);
            
            lastx = x;
            lasty = y;
        }
        
        g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
        g2.drawString(Double.valueOf(profit_max).toString(), w - PADDING + 5, PADDING + 4);
        g2.drawString(Double.valueOf(profit_min).toString(), w - PADDING + 5, h - PADDING + 4);
    }
 
    protected StockHistoryData findStockInStockList(ArrayList<StockHistoryData> h, String s)
    {
        for (int i = 0; i < h.size(); i++)
        {
            if (h.get(i).name.equals(s))
            {
                return h.get(i);
            }
        }
        return null;
    }

}
